package com.electronic.enums;

import lombok.Getter;
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
import org.bouncycastle.asn1.gm.GMObjectIdentifiers;
import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;

/**
 * @Author: IFCA
 * @Description: 算法枚举
 */
@Getter
public enum AlgorithmEnum {
    // 算法枚举
    SM3("SM3", "SM3withSM2", GMObjectIdentifiers.sm2sign_with_sm3),
    SHA256("SHA256", "SHA256withRSA", PKCSObjectIdentifiers.sha256WithRSAEncryption),
    SHA512("SHA512", "SHA512withRSA", PKCSObjectIdentifiers.sha512WithRSAEncryption);

    /**
     * 杂凑算法
     */
    private String hashAlg;
    /**
     * 签名算法
     */
    private String signAlg;

    /**
     * oid
     */
    private ASN1ObjectIdentifier asn1ObjectIdentifier;

    AlgorithmEnum(String hashAlg, String signAlg, ASN1ObjectIdentifier asn1ObjectIdentifier) {
        this.hashAlg = hashAlg;
        this.signAlg = signAlg;
        this.asn1ObjectIdentifier = asn1ObjectIdentifier;
    }

    public static AlgorithmEnum getAlgEnum(String hashAlg) {
        for (AlgorithmEnum algEnum : AlgorithmEnum.values()) {
            if (algEnum.getHashAlg().equalsIgnoreCase(hashAlg)) {
                return algEnum;
            }
        }
        return null;
    }

    public static AlgorithmEnum getAlgEnum(ASN1ObjectIdentifier asn1ObjectIdentifier) {
        for (AlgorithmEnum algEnum : AlgorithmEnum.values()) {
            if (algEnum.getAsn1ObjectIdentifier().getId().equals(asn1ObjectIdentifier.getId())) {
                return algEnum;
            }
        }
        return null;
    }
}
